package com.spooky.sync.persistence.dao;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;
import com.spooky.sync.persistence.db.CreateSqlSyntax;
import com.spooky.sync.persistence.db.DataType;
import com.spooky.sync.persistence.db.ExtendedDAOBean;
import com.spooky.sync.persistence.entity.BaseEntity;
import com.spooky.sync.persistence.entity.NoteTag;

public class NoteTagDAOBean extends ExtendedDAOBean<NoteTag> implements NoteTagDAO, CreateSqlSyntax {
	private static final String TAG = "NoteTagDAOBean";
	private static final String TABLE_NAME = "notetag";
	private static final String COL_FK_TAG = "nt_t_id";
	private static final String COL_FK_NOTE = "nt_n_id";
	protected Map<String, DataType> mCollums = new LinkedHashMap<String, DataType>();
	private TagDAO mTagDAO;
	private NoteDAO mNoteDAO;

	public NoteTagDAOBean(Context context) {
		super(context);
		mTagDAO = new TagDAOBean(context);
		mNoteDAO = new NoteDAOBean(context);
	}

	private NoteTagDAOBean() {
		super();
		initiate();
	}

	private void initiate() {
		mCollums.put(COL_FK_TAG, DataType.FOREIGNKEY);
		mCollums.put(COL_FK_NOTE, DataType.FOREIGNKEY);
	}

	@Override
	public NoteTag create(NoteTag noteTag) {
		return super.create(noteTag);
	}

	@Override
	public NoteTag update(NoteTag noteTag) {
		return super.updateEntity(noteTag);
	}

	@Override
	public void delete(NoteTag noteTag) {
		super.deleteEntity(noteTag);
	}

	@Override
	public List<NoteTag> find(NoteTag noteTag) {
		return new LinkedList<NoteTag>();
	}

	@Override
	protected void assign(BaseEntity entity, Cursor result) throws Exception {
		NoteTag noteTag = (NoteTag) entity;
		noteTag.setNote(mNoteDAO.find(result.getLong(result.getColumnIndexOrThrow(COL_FK_NOTE))));
		noteTag.setTag(mTagDAO.find(result.getLong(result.getColumnIndexOrThrow(COL_FK_TAG))));
	}

	@Override
	protected int assign(BaseEntity entity, SQLiteStatement statement) throws SQLException {
		NoteTag noteTag = (NoteTag) entity;
		int count = 1;
		statement.bindLong(count++, noteTag.getNote().getId());
		statement.bindLong(count++, noteTag.getTag().getId());
		return count++;
	}

	@Override
	public String getTableName() {
		return TABLE_NAME;
	}

	@Override
	public String getCreateSyntax() {
		Iterator<Entry<String, DataType>> iterator = mCollums.entrySet().iterator();
		StringBuffer sql = new StringBuffer();
		sql.append("CREATE TABLE ");
		sql.append(TABLE_NAME);
		sql.append(" (\n");
		while (iterator.hasNext()) {
			Entry<String, DataType> next = iterator.next();
			sql.append(next.getKey());
			sql.append(" ");
			sql.append(next.getValue().getDataType());
			sql.append(",\n");
		}
		sql.delete(sql.length() - 2, sql.length());
		sql.append("\n);\n");
		return sql.toString();
	}

	@Override
	public String[] getCollumns() {
		String[] collumns = new String[mCollums.size()];
		Iterator<String> iterator = mCollums.keySet().iterator();
		int i = 0;
		while (iterator.hasNext()) {
			String next = iterator.next();
			collumns[i++] = next;
		}
		return collumns;
	}

	@Override
	protected NoteTag getEntity() {
		return new NoteTag();
	}

	@Override
	public String getChangeSyntax(int version) {
		return mSyntaxUpdate.get(version);
	}
}
